/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.service.cronjob.autocheck.impl;

import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.location.dt.enums.RefundReasonEnum;
import com.ceair.flight.refund.service.cronjob.autocheck.IScheduleChangeChecker;
import com.ceair.flight.refund.service.cronjob.autocheck.ITicketIssuedIn24HChecker;
import com.ceair.flight.refund.service.cronjob.validator.IValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 * @author youguilin
 */
@Service("involuntaryChecker")
@Order(value = 1000)
public class InvoluntaryChecker extends BaseValidatorChecker {

    private static final Logger LOG = LoggerFactory.getLogger(InvoluntaryChecker.class);
    @Resource(name = "scheduleChangeChecker")
    private IScheduleChangeChecker scheduleChangeChecker;
    @Autowired
    private ITicketIssuedIn24HChecker issueIn24HChecker;
    @Resource(name = "basicInvoluntaryValidator")
    private IValidator basicInvoluntaryValidator;

    public InvoluntaryChecker() {
        super("involuntaryChecker");
    }

    @Override
    public boolean autoCheck(RefundApplyDto refundApply) {
        LOG.debug("Refund apply {} checking flight reschedule", refundApply.getId());
        RefundReasonEnum reasonEnum = super.getRefundReason(refundApply);
        //航变检查
        if (RefundReasonEnum.FLIGHT_RESCHEDULE == reasonEnum) {
            LOG.info("Refund apply {} apply reason is {}", refundApply.getId(), RefundReasonEnum.FLIGHT_RESCHEDULE.name());
            return scheduleChangeChecker.isScheduleChangeFlight(refundApply) && validate(refundApply);
        } else if (RefundReasonEnum.REFUND_IN_24H == reasonEnum) {
            LOG.info("Refund apply {} apply reason is {}", refundApply.getId(), RefundReasonEnum.REFUND_IN_24H.name());
            return issueIn24HChecker.checkIssuedIn24H(refundApply) && validate(refundApply);
        } else if (!reasonEnum.isVoluntary()) {
            //其他非自愿退票, 需手工审核
            return false;
        }
        return goNextOrReturn(refundApply);
    }

    @Override
    IValidator getValidator(RefundApplyDto refundApply) {
        return basicInvoluntaryValidator;
    }

}
